# 多线程读取数据版本
import requests
import time
import asyncio
import aiohttp
import nest_asyncio
headers= {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'
}
async def download_one(url):
    #resp = requests.get(url)
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers) as resp:
            print('Read {} from {}'.format(resp.content_length,url))
async def download_all(sites):
    tasks = [asyncio.create_task(download_one(site)) for site in sites]
    await asyncio.gather(*tasks)
    # for site in sites:
        
            #download_one(site)
def main():
    sites = [
        'https://www.baidu.com/s?wd=周杰伦',
        'https://www.baidu.com/s?wd=周饼伦',
        'https://www.baidu.com/s?wd=周口伦',
        'https://www.baidu.com/s?wd=黑伦',
        'https://www.baidu.com/s?wd=粥饼伦',
        'https://www.baidu.com/s?wd=雍杰伦',
        'https://www.baidu.com/s?wd=孙冬',
        'https://www.baidu.com/s?wd=周口同学',
        'https://www.baidu.com/s?wd=上海杰伦老师',
        'https://www.baidu.com/s?wd=方文山',
        'https://www.baidu.com/s?wd=马云',
    ]
    start_time = time.perf_counter()
    #nest_asyncio.apply()
    asyncio.run(download_all(sites))  # 改用asyncio.run()函数代替asyncio.get_event_loop().run_until_complete()
    end_time = time.perf_counter()
    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))
if __name__ == '__main__':
    main()